<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserOrderController extends Controller
{
    public static $str="";
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user=\Cookie::get('user');
        $orders=\DB::table("orders")->where("uid",$user['id'])->groupBy('orders.code')->get();
        // dump($orders);
        foreach($orders as $k=>$v){
            $goods=\DB::table("goods")->where("id",$v['gid'])->first();
            $orders[$k]['img']=$goods['img'];
            $orders[$k]['price2']=$goods['price'];
            $orders[$k]['title']=$goods['title'];
        }

        foreach($orders as $k =>$v){
            $order=\DB::table("orders")->where("code",$v['code'])->get();
            // dump($order);
                foreach($order as $kk => $value){
                    $goods=\DB::table("goods")->where("id",$value['gid'])->first();

                    $orders[$k]['order'][$kk]="<ul class='clearfix table_pro'><li class='table_proimg'><a href='/goods/".$value['gid']."' ><img src=".$goods['img']." ></a></li><li class='table_proname ' style='height: auto;line-height: 18px;padding-top: 40px '><p><a href='/goods/".$value['gid']."' target='_blank '>".$goods['title']."</a><br></p></li> <li class='table_proprice '>".$goods['price']."</li><li class='table_pronum '>".$v['num']."</li></ul>";
                }
        }
        return view("home.user.userorder",['orders'=>$orders]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // return 123;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id,Request $request)
    {
        $code=$request->all()['code'];
        return $code;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


      public function hhh($code)
    {
        //
        //
        // dd($code);
        $data=array();
        $data['code']=$code;
        $orders=\DB::table("orders")->where("code",$code)->get();
        $price=0;
        for($i=0;$i<count($orders);$i++){
            $num=$orders[$i]['price'] * $orders[$i]['num'];
                $price+=$num;
        }

        $aid=$orders[0]['aid'];
        $ordertime=$orders[0]['time'];
        $addr=\DB::table('addr')->where('id',$aid)->first();
        $data['price']=$price;


      $info=\DB::table("orders")
                ->select("orders.*","goods.goodsname","goods.img")
                ->join("goods","goods.id","=","orders.gid")
                ->where("code",$code)->get();

       return view("home.user.userorders")->with('data',$data)->with('info',$info)->with('addr',$addr)->with('ordertime',$ordertime);
    }
}
